package leetcode_周赛._2022._202206;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/6/5 10:56
 */
public class _05 {
    public static void main(String[] args) {
        final TextEditor textEditor = new TextEditor();
        textEditor.addText("leetcode");
        System.out.println(textEditor.deleteText(4));
        textEditor.addText("practice");
        System.out.println(textEditor.cursorRight(3));
        System.out.println(textEditor.cursorLeft(8));
        System.out.println(textEditor.deleteText(10));
        System.out.println(textEditor.cursorLeft(2));
        System.out.println(textEditor.cursorRight(6));
    }

    /**
     * 6092. 替换数组中的元素
     * 使用两个 Deque 来完成
     * 第一个 Deque 存储指针左边的数据
     * 第二个 Deque 存储指针右边的数据
     *
     * @param nums
     * @param operations
     * @return
     */
    public int[] arrayChange(int[] nums, int[][] operations) {
        int n = nums.length;
        Map<Integer, List<Integer>> map = new HashMap<>(n);
        for (int i = 0; i < n; i++) {
            List<Integer> list = map.getOrDefault(nums[i], new ArrayList<>());
            list.add(i);
            map.put(nums[i], list);
        }
        for (int[] operation : operations) {
            if (!map.containsKey(operation[0])) continue;
            List<Integer> list = map.get(operation[0]);
            for (int i : list) nums[i] = operation[1];
            List<Integer> ll = map.getOrDefault(operation[1], new ArrayList<>());
            ll.addAll(list);
            map.put(operation[1], ll);
            map.remove(operation[0]);
        }
        return nums;
    }

    /**
     * cursor 指向 -1 表示 cursor 的左边没有字符
     */
    static class TextEditor {
        StringBuilder sb;
        int cursor = -1;

        public TextEditor() {
            sb = new StringBuilder();
        }

        public void addText(String text) {
            StringBuilder s1 = new StringBuilder();
            if (cursor != -1) for (int i = 0; i <= cursor; i++) s1.append(sb.charAt(i));
            StringBuilder s2 = new StringBuilder();
            for (int i = cursor + 1; i < sb.length(); i++) s2.append(sb.charAt(i));
            cursor += text.length();
            s1.append(text).append(s2);
            sb = s1;
        }

        public int deleteText(int k) {
            int res;
            if (cursor + 1 < k) {
                sb.delete(0, cursor + 1);
                res = cursor + 1;
                cursor = -1;
            } else {
                sb.delete(cursor - k + 1, cursor + 1);
                cursor -= k + 1;
                res = k;
            }
            return res;
        }

        public String cursorLeft(int k) {
            if (cursor + 1 < k) {
                cursor = -1;
                return "";
            }
            cursor -= k + 1;
            int len = Math.min(10, cursor + 1);
            return sb.substring(cursor - len + 1, cursor + 1);
        }

        public String cursorRight(int k) {
            int leftLen = sb.length() - cursor - 1;
            if (leftLen < k) {
                cursor = sb.length() - 1;
                return "";
            }
            cursor += k;
            int len = Math.min(10, cursor + 1);
            return sb.substring(cursor - len + 1, cursor + 1);
        }
    }

}
